//
//  MJTTabBarViewController.swift
//  swiftWeiBo
//
//  Created by MJT on 16/7/9.
//  Copyright © 2016年 MJT. All rights reserved.
//

import UIKit
import SVProgressHUD

class MJTTabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //设置添加tabbar
        let addTabBar = MJTTabBar()
        //设置代理
        addTabBar.MJTDelegate = self
        //执行闭包,weak self]解决循环引用
        addTabBar.compeseBttonClosure = { [weak self] in

            if !MJTUserAccountViewModel.shareUserAccount.isLogin {
                SVProgressHUD.showErrorWithStatus("请先登录!")
                return
            }
            //若登录
//            let window = UIApplication.sharedApplication().windows.last!
//            let view = MJTComposeView()
//            window.addSubview(view)
            if let weakSelf = self {
                let composeView = MJTComposeView()
                composeView.show(weakSelf)
            }
        }
        //利用kvc给只读属性赋值
        setValue(addTabBar, forKey: "tabBar")

        //添加首页tabBarItem
        addChildViewController(MJTHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        //添加消息tabBarItem
        addChildViewController(MJTMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        //添加发现tabBarItem
        addChildViewController(MJTDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        //添加我的tabBarItem
        addChildViewController(MJTPrefileTableViewController(), title: "我的", imageName: "tabbar_profile")
    }
    //重载
    func addChildViewController(childController: UIViewController ,title: String ,imageName: String) {

        childController.tabBarItem = MJTTabBarItem()
        //设置title
        childController.title = title
        //设置图片
        childController.tabBarItem.image = UIImage(named: imageName)
        //设置选择状态图片
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")
        //设置渲染状态
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //设置渲染颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        //设置字体
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont .systemFontOfSize(12)], forState: .Normal)
        //添加子视图控制器
        let nav = UINavigationController(rootViewController: childController)
        //添加子视图显示
        addChildViewController(nav)
    }

}
 // MARK: 实现代理方法
extension MJTTabBarViewController: MJTTabBarDelegate {
    func didSelectedComposeButton() {
//        print("代理传过来的撰写点击事件")
    }
}